package com.controllers.forms;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.config.SimplePropertyFilter;
//import com.config.SimplePropertyFilter;

/**
 * Created by Administrator on 2017/5/9.
 */
public class SimpleResponse {

    private Boolean success;
    private Object result;
    private ResponseError error = new ResponseError();
    private ResponsePaginator paginator = new ResponsePaginator();

    public SimpleResponse(Boolean success, Object result) {
        this.success = success;
        this.result = result;
    }

    public SimpleResponse(Boolean success, String message) {
        this.success = false;
        this.error.setMessage(message);
    }

    public SimpleResponse(Boolean success, Object result, PaginatorForm paginatorForm) {
        this.success = success;
        this.result = result;
        this.paginator.total = paginatorForm.getTotal();
    }

    public static JSONObject ok(Object result) {
        return returnResult(new SimpleResponse(true, result));
    }

    public static JSONObject fail(String message) {
        return returnResult(new SimpleResponse(true, message));
    }

    public static JSONObject ok(Object result, PaginatorForm paginatorForm) {
        return returnResult(new SimpleResponse(true, result, paginatorForm));
    }

    private static JSONObject returnResult(SimpleResponse response) {
        SimplePropertyFilter filter = new SimplePropertyFilter();
        SerializerFeature feature = SerializerFeature.DisableCircularReferenceDetect;
        String str = JSON.toJSONString(response, filter, feature);
//        String str = JSON.toJSONString(response);
        return JSON.parseObject(str);
    }

    public static class ResponsePaginator {
        private Long total;

        public Long getTotal() {
            return total;
        }

        public void setTotal(Long total) {
            this.total = total;
        }
    }


    public static class ResponseError {
        private String message;

        public String getMessage() {
            return message;
        }

        public void setMessage(String message) {
            this.message = message;
        }
    }


    public Boolean getSuccess() {
        return success;
    }

    public void setSuccess(Boolean success) {
        this.success = success;
    }

    public Object getResult() {
        return result;
    }

    public void setResult(Object result) {
        this.result = result;
    }

    public ResponseError getError() {
        return error;
    }

    public void setError(ResponseError error) {
        this.error = error;
    }

    public ResponsePaginator getPaginator() {
        return paginator;
    }

    public void setPaginator(ResponsePaginator paginator) {
        this.paginator = paginator;
    }
}
